import defineReactive from './defineReactive.js'
import Dep from './dep.js'
import observe from './observe.js'
import protoArgument from './protoArgument.js'

/*
 * @Author: zengwj
 * @Date: 2022-08-08 17:37:26
 * @LastEditors: zengwj
 * @LastEditTime: 2022-08-10 09:47:45
 * @Description:
 */
/**
 * 为普通对象或者数组设置响应式的入口
 */
export default function Observer(value) {
	// 为对象本身设置一个 dep，方便在更新对象本身时使用，比如 数组通知依赖更新时就会用到
	this.dep = new Dep()
	Object.defineProperty(value, '__ob__', {
		value: this,
		enumerable: false,
		writable: true,
		configurable: true,
	})
	if (Array.isArray(value)) {
		//拦截数组方法
		protoArgument(value)
		this.observeArray(value)
	} else {
		this.walk(value)
	}
}

Observer.prototype.walk = function (obj) {
	for (let key in obj) {
		defineReactive(obj, key, obj[key])
	}
}

Observer.prototype.observeArray = function (arr) {
	for (let item of arr) {
		observe(item)
	}
}
